home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1412 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.2 KB  |  108 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: locking
  5. Date: 10 Jan 1996 21:33:29 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan10223330@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4d0j6r$1ri@daphne.ecmwf.int>
  10.     <30F3E9C3.15FB7483@intellektik.informatik.th-darmstadt.de>
  11.     <NITIN.96Jan10103013@more.eng.sun.com>
  12. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  13. In-reply-to: nitin@more.eng.sun.com's message of 10 Jan 1996 18:30:13 GMT
  14.  
  15. In article <NITIN.96Jan10103013@more.eng.sun.com> nitin@more.eng.sun.com (Nitin More [CONTRACTOR]) writes:
  16.  
  17.    In article <30F3E9C3.15FB7483@intellektik.informatik.th-darmstadt.de> Enno Sandner <enno@intellektik.informatik.th-darmstadt.de> writes:
  18.  
  19.    > From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  20.  
  21.    [deleted]
  22.  
  23.    > > 
  24.    > > This seems to work, but the LockObject is only destroyed at the
  25.    > > end of the block, locking my object for too long.
  26.    > > 
  27.    > > main()
  28.    > > {
  29.    > > 
  30.    > >         SharedObject<foo> fooH("lock");
  31.    > > 
  32.    > >         ... the lock is not set
  33.    > > 
  34.    > >         fooH->bar();
  35.    > > 
  36.    > >         .. the lock is set until the end.
  37.    > > }
  38.    > > 
  39.    > > Anyone got an idea ?
  40.    > > 
  41.    > 
  42.    > I would say the temporary object should be destroyed directly after
  43.    > the invocation of 'bar'.
  44.    > As a workarround you can put the member-function call in brackets, e.g.
  45.    > 
  46.    >             { foo->bar(); }
  47.    > 
  48.    >     Enno
  49.  
  50.    You need to put the declaration of fooH also in the block as follows:
  51.  
  52.        {
  53.        SharedObject<foo> fooH("lock");
  54.        fooH->bar();
  55.        .. the lock is set until the end **OF THE BLOCK**
  56.        }
  57.        .. fooH is destructed at the end of the block releasing the lock.
  58.  
  59. The original poster proposes an approach that builds upon chaining wrapper
  60. classes. He has a class 'LockObject' the maintains the lock-state of a
  61. specific object:
  62.     
  63.     template<class T> class LockObject {
  64.        public:
  65.         LockObject(T* o) : object(o) { lock(); }
  66.         ~LockObject() { unlock(); }
  67.         T* operator -> () { return object; }
  68.         ...
  69.             T* object;
  70.         };
  71.  
  72. and a class that delegates method invocations to the appropriate member-
  73. functions. The trick is that he doesn't return the object directly but 
  74. wraps it in an instance of 'LockObject'. Thus it's guaranteed that the lock
  75. exists before the member-function is called and will be removed sometimes
  76. after the member-function has been invoked.
  77.  
  78.     template<class T> class SharedObject {
  79.             ...
  80.         LockObject<T> operator -> () { 
  81.         return LockObject<T>(object); 
  82.         }
  83.             ...
  84.        T* object;
  85.     };
  86.  
  87. For the class
  88.  
  89.     struct Foo { void bar(); }
  90.  
  91. a method-invocation like: 
  92.  
  93.     Foo f;
  94.     SharedObject<Foo> s(&f);
  95.     s->bar(); // (1)
  96.  
  97. results in 
  98.         
  99.       ((s.operator->()).operator->())->bar(); // = (1)
  100.  
  101. IMHO, the 'LockObject' temporary created in this expression should be
  102. destroyed after this line. To force this behavior from an unwilling
  103. compiler (;-) one can put (1) in brackets. So the brackets are used to
  104. restrict the lifetime of the temporary and not the one of the SharedObject
  105. instance.
  106.  
  107.     Enno
  108.